package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /* 1.接受前端传递用户名和密码
 2.根据用户名查询employee对象；
             3.判断employee对象是否为null ：
             4.如果为null:给前端响应用户名不存在
 5.如果不为null:
     校验密码；
             5.1 对前端传递的明文密码进行加密
        5.2 对加密的结果和数据库中密文进行对比
     不equals:密码错误：给前端响应密码错误
     equals:密码正确
     判断status是否0：
             0：账号禁用，给前端响应账户已经被禁用的信息
                            1:   账号正常
             将用户信息存储session
     给前端响应登录成功信息*/

    /**
     * 登录
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpServletRequest request) {

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        wrapper.eq("username", employee.getUsername());
        //根据用户名查询employee对象；
        Employee e = employeeService.getOne(wrapper);
        if (e == null) {
            return R.error("用户名不存在");
        }
        //对前端密码进行加密
        String password = employee.getPassword();
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        //判断密码是否正确
        if (s.equals(e.getPassword())) {
            if (e.getStatus() == 1) {
                request.getSession().setAttribute("qq", e.getId());
                return R.success(e);
            } else {
                return R.error("账号已禁用");
            }
        }
        return R.error("密码错误");
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R drop(HttpServletRequest request) {
        //用户退出结束此次会话
        HttpSession session = request.getSession();
        session.invalidate();

        return R.success(null);
    }

    /**
     *分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R selectAll(int page, int pageSize, String name) {
        //分页查询
        Page<Employee> pages = new Page<>(page, pageSize);

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        //动态sql判断name是否为空
            //如果不为空模糊查询
            wrapper.like(StringUtils.isNotEmpty(name),"name", name);
        //反之分页查询
        Page<Employee> page1 = employeeService.page(pages, wrapper);
       // log.info(page1.toString());

        return R.success(pages);

    }

    /**
     * 添加
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    public R Add(@RequestBody Employee employee, HttpServletRequest request) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        wrapper.eq("username", employee.getUsername());
        //根据用户名查询employee对象；
        Employee e = employeeService.getOne(wrapper);
        if (e == null) {
            String s = DigestUtils.md5DigestAsHex("123456".getBytes());

            employee.setPassword(s);
//            employee.setCreateTime(LocalDateTime.now());
//            employee.setUpdateTime(LocalDateTime.now());
//            //获得当前登录用户的id
//            Long empId = (Long) request.getSession().getAttribute("qq");
//            employee.setCreateUser(empId);
//            employee.setUpdateUser(empId);
            employeeService.save(employee);
            return R.success("添加成功");
        }
        return R.error("用户名存在,去瞅瞅");
    }

    /**
     * 修改
     * @param employee
     * @param request
     * @return
     */
    @PutMapping
    public R update(@RequestBody Employee employee,HttpServletRequest request){

//        employee.setUpdateTime(LocalDateTime.now());
//        Long qq = (Long) request.getSession().getAttribute("qq");
//        employee.setUpdateUser(qq);

        employeeService.updateById(employee);

        return R.success(null);

    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectId(@PathVariable("id") Long id){

        Employee byId = employeeService.getById(id);


        return R.success(byId);

    }

}
